package com.yunpuvip.sachet.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.yunpuvip.sachet.biz.entity.Player;
import com.yunpuvip.sachet.biz.model.params.PlayerParam;
import com.yunpuvip.sachet.biz.service.PlayerService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.yunpuvip.sachet.core.common.exception.BizExceptionEnum;
import com.yunpuvip.sachet.modular.core.exception.GunsException;
import com.yunpuvip.sachet.modular.core.exception.GunsExceptionEnum;
import com.yunpuvip.sachet.modular.core.exception.ServiceExceptionEnum;
import com.yunpuvip.sachet.modular.page.LayuiPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sql.rowset.serial.SerialException;
import java.rmi.ServerException;
import java.rmi.server.ServerCloneException;


/**
 * 会员用户控制器
 *
 * @author wushaohong
 * @Date 2019-08-10 09:46:09
 */
@Controller
@RequestMapping("/player")
public class PlayerController extends BaseController {

    private String PREFIX = "/modular/player";

    @Autowired
    private PlayerService playerService;

    /**
     * 跳转到主页面
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/player.html";
    }

    /**
     * 新增页面
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/player_add.html";
    }

    /**
     * 编辑页面
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/player_edit.html";
    }

    /**
     * 新增接口
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(PlayerParam playerParam) {
        if(null!=playerParam&& StrUtil.isNotEmpty(playerParam.getPhone())){
            Player playerByPhone = playerService.getPlayerByPhone(playerParam.getPhone());
            if(ObjectUtil.isNotNull(playerByPhone)){
                throw new GunsException(GunsExceptionEnum.USER_EXIST);
            }
        }
        String s = playerService.genUserNo();
        playerParam.setUserNo(s);
        playerParam.setUserhead("/img/default.jpg");
        this.playerService.add(playerParam);

        return ResponseData.success();

    }

    /**
     * 编辑接口
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(PlayerParam playerParam) {
        this.playerService.update(playerParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(PlayerParam playerParam) {
        this.playerService.delete(playerParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(PlayerParam playerParam) {
        Player detail = this.playerService.getById(playerParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wushaohong
     * @Date 2019-08-10
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(PlayerParam playerParam) {
        return this.playerService.findPageBySpec(playerParam);
    }

}


